<template>
  <div class="role-container">
    <el-row>
      <el-col>
        <el-button type="primary" @click="addRoles">新增角色</el-button>
      </el-col>
    </el-row>
    <el-table :data="roleData" border>
      <el-table-column label="Role Key" prop="roleKey" width="200"></el-table-column>
      <el-table-column label="Role Name" prop="roleName" width="200"></el-table-column>
      <el-table-column label="Description" prop="Description"></el-table-column>
      <el-table-column label="OPerations">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="addRoles(scope.row)">编辑权限</el-button>
          <el-button type="danger" size="small" @click="removeRole(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="New Role" :visible.sync="isAddRoleDialogVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="addRoleData" ref="addRoleFormRef" :rules="addRoleFormRules" label-width="70px">
        <el-form-item label="Name" prop="roleName">
          <el-input v-model="addRoleData.roleName" placeholder="Role name"></el-input>
        </el-form-item>
        <el-form-item label="Desc" prop="roleDesc">
          <el-input v-model="addRoleData.roleDesc" placeholder="Role Description"></el-input>
        </el-form-item>
        <el-form-item label="Menus" prop="roleMenus">
          <el-tree
            :data="navList"
            show-checkbox
            ref="treeRef"
            default-expand-all
            :default-checked-keys="keyId"
            node-key="id"
            :props="defaultProps">
          </el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddRoleDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addRole">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleData: [
        {
          roleKey: 'admin',
          roleName: 'admin',
          Description: 'Super Administrator. Have access to view all pages.'
        },
        {
          roleKey: 'editor',
          roleName: 'editor',
          Description: 'Normal Editor. Can see all pages except permission page.'
        },
        {
          roleKey: 'vistior',
          roleName: 'visitor',
          Description: 'Just a visitor. Can only see the home page and the document page'
        }
      ],
      isAddRoleDialogVisible: false,
      addRoleData: {
        roleName: '',
        roleDesc: '',
        roleMenus: [],
        type: 'add'
      },
      addRoleFormRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 3, max: 12, message: '请输入合法的名称', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 6, max: 100, message: '请输入合法的描述信息', trigger: 'blur' }
        ],
        roleMenus: [
        ]
      },
      navList: [],
      keyId: [],
      defaultProps: {
        label: 'navName',
        children: 'children'
      }
    }
  },
  methods: {
    async addRoles(info) {
      this.isAddRoleDialogVisible = true
      this.keyId = info.power
      console.log(info)
      const res = await this.$http.get('/home')
      this.navList = res
      this.addRoleData.type = 'add'
      if(info) {
        this.addRoleData.roleName = info.roleName
        this.addRoleData.roleDesc = info.Description
        this.addRoleData.type = 'edit'
      }
    },
    addRole() {
      this.addRoleData.roleMenus = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      this.$refs.addRoleFormRef.validate(valid => {
        if(!valid) return
        if(this.addRoleData.type === 'add') {
          const role = {
            roleKey: 'editor',
            roleName: this.addRoleData.roleName,
            Description: this.addRoleData.roleDesc
          }
          this.roleData.push(role)
        }else if(this.addRoleData.type === 'edit') {
          this.roleData.find(item => {
            if(item.roleName === this.addRoleData.roleName) {
              item.Description = this.addRoleData.roleDesc
              item.power = this.addRoleData.roleMenus
              return true
            }
            return false
          })
        }
        this.isAddRoleDialogVisible = false
      })
    },
    async removeRole(info) {
      const confirm = await this.$confirm('确定要删除该角色吗？', '删除角色提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if(confirm !== 'confirm') return this.$message({ type: 'info', message: '取消了删除角色操作', duration: 2000 })
      this.roleData = this.roleData.filter(item => {
        return item.roleName !== info.roleName
      })
    }
  }
}
</script>

<style>
  .role-container {
    margin: 20px;
  }
  .el-row {
    margin: 20px 0;
  }
</style>
